package com.formina.stars;


import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.Matrix;
import android.os.SystemClock;
import android.util.Log;

import com.formina.engine.Engine;
import com.formina.engine.Model;



public class Comet extends Model {

		public static int GROWING = 0;
		public static int IDLE = 1;
		public static int ANIMATING = 2;
		
		private int rotationAxis = 0;
		private int mode = 0;
		private float[] growingScale;
		
		private long[] animations = {0,100,200,300,400,500};
		private long animationStart = 0;
		Random rand = new Random();
		private float[] streamColor;
		
		List<CometStreamer> Streamers = new ArrayList<CometStreamer>();
	 
       public Comet(float[] location, float[] scale){
    	   super(location, scale, Engine.StarParticle);
    	   this.speed = 5f;
    	   streamColor = this.getRandomColor();
    	   this.rotationAxis=2;
       }
	   
	   public Comet() {
		  this(new float[]{0,0,0}, new float[]{1f,1f,1f});
	   }
	   
	   long startTime = 0;
	   @Override
	   public void update(long time){
		   
		   if (lastTime == 0){
			   lastTime = time;
		   }
		   
		   
		   //setAlpha(Color()[3]-.02f);;
		   setX(getX()+getVelocity()[0]* getSpeed());
		   setY(getY()+getVelocity()[1]* getSpeed());
		   setZ(getZ()+getVelocity()[2]* getSpeed());
		   setAlpha(1f);
		   Rotation()[rotationAxis] += 4f;
		   if (Rotation()[rotationAxis]>359){
			   Rotation()[rotationAxis]=0;
		   }
		   
		   
		   if(!isDead){
			   if(time-startTime > 500){
				   if(getX()<0){
					   this.isDead = true;
				   }else if(getX()>Engine.WIDTH){
					   this.isDead = true;
				   }else if(getY()<0){
					   this.isDead = true;
				   }else if(getY()>Engine.HEIGHT){
					   this.isDead = true;
				   }
			   }
			   float tempscale = 7f;
			   if(time>lastTime+50){
				   CometStreamer s = new CometStreamer(this.Location(),new float[]{tempscale,tempscale,tempscale});
				   s.setColor(this.streamColor);
				   Streamers.add(s);
				   lastTime=time;
			   }
			   if(Streamers.size()>0){
				   for(int x= Streamers.size()-1; x>=0;x--){
					   CometStreamer c = Streamers.get(x);
					   if(c.IsDead()){
						   Streamers.remove(x);
					   }else{
						   c.update(time);
					   }
					}
			   }
			   
			   
		   }
		   
		   /*
		   if(!isDead){
			   if(time-lastTime>1000){
				   	  CometStreak c = new CometStreak(this.location.clone(),new float[]{1f,1f,0f});
				   	  c.setDirection(90f);
					  Engine.Models.add(c);
					  lastTime=time;
			   }
		   }
		   */
		  
		   
	   }
	   
	   @Override
	    public void draw(GL10 gl) {

			
	        
	     // Use culling to remove back faces.
	        GLES20.glEnable(GLES20.GL_CULL_FACE);
	        GLES20.glBlendFunc (GLES20.GL_ONE, GLES20.GL_ZERO);	         

	        // Enable depth testing
	        GLES20.glEnable(GLES20.GL_DEPTH_TEST);
	        
	        Matrix.setIdentityM(mModelMatrix, 0);
	        Matrix.setIdentityM(rotationMatrix, 0);
	        Matrix.setIdentityM(scaleMatrix, 0);
	        Matrix.translateM(mModelMatrix, 0, this.getX(), this.getY(), this.getZ());
	        Matrix.scaleM(mModelMatrix, 0, Scale()[0], Scale()[1],Scale()[2]);
	        Matrix.rotateM(mModelMatrix, 0, this.Rotation()[this.rotationAxis], 0f, 0f, 1f);

	        
	        GLES20.glVertexAttrib3fv(Engine.mLightPositionHandle, new float[] {getX()-50, getY()+100, getZ()+100}, 0);
	        
	        //position
	        modelInfo.vertexBuffer.position(0);
	        GLES20.glVertexAttribPointer(Engine.mPositionHandle, 3, GLES20.GL_FLOAT, false,
	        		0, modelInfo.vertexBuffer);
			GLES20.glEnableVertexAttribArray(Engine.mPositionHandle);
	        //end position
			
			//color
	        GLES20.glVertexAttrib4f(Engine.mColorHandle, this.Color()[0],this.Color()[1],this.Color()[2],this.Color()[3]);

			//GLES20.glVertexAttribPointer(Engine.mColorHandle, 3, GLES20.GL_FLOAT, false,
	        		//0, this.ColorBufferV());    
	        //GLES20.glEnableVertexAttribArray(Engine.mColorHandle);
			//end color
	        
	        //normals for lighting
	        GLES20.glVertexAttribPointer(Engine.mNormalHandle, 3, GLES20.GL_FLOAT, false, 
	        		0, this.modelInfo.normalBuffer);
	        GLES20.glEnableVertexAttribArray(Engine.mNormalHandle);

	        //end normals
				       	        
	        Matrix.multiplyMM(Engine.mMVPMatrix, 0, Engine.mVMatrix, 0, mModelMatrix, 0);
	        GLES20.glUniformMatrix4fv(Engine.mMVMatrixHandle, 1, false, Engine.mMVPMatrix, 0);

	        Matrix.multiplyMM(Engine.mMVPMatrix, 0, Engine.mProjMatrix, 0, Engine.mMVPMatrix, 0);
	        GLES20.glUniformMatrix4fv(Engine.mMVPMatrixHandle, 1, false, Engine.mMVPMatrix, 0);
	        
	        // Pass in the light position in eye space.        
	        GLES20.glUniform3f(Engine.mLightPosHandle, Engine.mLightPosInEyeSpace[0], Engine.mLightPosInEyeSpace[1], Engine.mLightPosInEyeSpace[2]);
	        
	        //color
	        //GLES20.glUniform4fv(Engine.mColorHandle, 1, this.ColorBuffer());
	        //end color
	        
	        GLES20.glDrawElements(GLES20.GL_TRIANGLES, modelInfo.Indices.length,// OpenGL docs
					GLES20.GL_UNSIGNED_SHORT, modelInfo.indexBuffer);
	        
	        GLES20.glDisable(GLES20.GL_CULL_FACE);
	        GLES20.glDisable(GLES20.GL_DEPTH_TEST);
	        				
	        if(Streamers.size()>0){
				   for(int x= Streamers.size()-1; x>=0;x--){
						Streamers.get(x).draw(gl);
					}
			   }
	   }
	   
	}

